import { cartList } from '@/api/cart'
const state = {
  cartAmount: 0,
  goodsName: ''
}

const mutations = {
  'SET_CART_AMOUNT' (state, count) {
    state.cartAmount = count
  },
  'GET_GOODS_NAME' (state, name) {
    state.goodsName = name
  }
}

const actions = {
  // 购物车
  async fetchCarts ({ commit }) {
    const res = await cartList()
    if (res.length) {
      const count = res.reduce((pre, cur) => {
        return pre + cur.amount
      }, 0)
      commit('SET_CART_AMOUNT', count)
    }
  },
  getGames ({ commit }, name) {
    commit('GET_GOODS_NAME', name)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
